﻿@{
    ViewBag.Title = "List";
    Layout = "~/Views/Shared/_PageLayout.cshtml";
}
@model Beway.ValueAdded.Model.AdminUser
@section HeadSection
{
    <script type="text/javascript">
        var url = "";
        $(function () {
            $('#Managerlist').datagrid({
                title: '管理员管理',
                iconCls: 'icon-edit',
                url: '/Manager/ManagerList',
                striped: true,
                idField: 'AdminID',
                border: true,
                loadMsg: '数据加载中...',
                singleSelect: true,
                pagination: true,
                pageSize: 10,
                pageList: [10, 20],
                frozenColumns: [[{ field: 'ck', checkbox: true}]],
                columns: [[
                        { field: 'AdminUserName', title: '用户名', width: 100 },
                        {
                            field: 'RoleID', title: '角色', width: 120, formatter: function (val) {
                                if (val === 0)
                                {
                                    return "<span style='color:red;'>系统管理员</span>";
                                }
                                else
                                {
                                    return "<span style='color:green;'>充值账户管理员</span>"
                                }
                            }
                        },
                         { field: 'TrueName', title: '姓名', width: 100 },
                         { field: 'Mobile', title: '手机号', width: 100 },
                        //{ field: 'MerchantTitle', title: '所属商户', width: 120 },
                        { field: 'LastLoginTime', title: '最后登录时间', width: 150, formatter: function (val, rec) {
                            return JSONDateToJSDate(val);
                        }
                        },
                    { field: 'LastLoginIP', title: '最后登录IP', width: 120 },
                          { field: 'LoginDisabled', title: '账户状态', width: 80, align: 'center', formatter: function (val, rec) {
                              if (val == true) { val = "<font color='red'>禁用</font>"; } else { val = "<font color='green'>启用</font>"; };
                              return val;
                          }
                          },
                    { field: 'AddTime', title: '添加时间', width: 120, formatter: function (val, rec) {
                        return JSONDateToJSDate(val);
                    }
                    }

                        ]], toolbar: [{ text: '新增',
                            iconCls: 'icon-add',
                            handler: function () {
                                newManager();
                            }
                        }, '-', { text: '编辑',
                            iconCls: 'icon-edit',
                            handler: function () {
                                editManager();
                            }
                        }, '-', { text: '删除',
                            iconCls: 'icon-remove',
                            handler: function () {
                                delManager();
                            }
                        }, '-']
            });
//            var p = $('#Managerlist').datagrid('getPager');
//            $(p).pagination({
//                onBeforeRefresh: function () {
//                    $(this).pagination('loading');
//                    $(this).pagination('loaded');
//                }
//            });

            //尺寸
            $('#Managerlist').datagrid('resize');

            //加载搜索框//在这个样式下。加载搜索框的div
            $(".datagrid-toolbar").append($("#divsearch"));

            $("#radioRole1").click(function () {
                if ($(this).attr("checked") == "checked") {
                    $("#MerID").removeAttr("disabled");
                }
            });

            $("#radioRole0").click(function () {
                if ($(this).attr("checked") == "checked") {
                    $("#MerID").attr("disabled", "disabled");
                    $("#MerID").val(0);
                }
            });

        });

        //搜索
        function doSearch() {
            $('#Managerlist').datagrid('load', { UserName: $("#UserName").val() });
        }

        //新增
        function newManager() {
            $("#DivUserName").show();
            url = "/Manager/AddManager";
            $("#dlg").dialog('open').dialog('setTitle', '添加管理员信息');
            $("#fm").form('clear');

            $("#MerID").removeAttr("disabled");

            $('#AdminPwd').validatebox({ required: true });
            $("#pwdtip").html("");

        }
        //新增确定
        function ManagerSave() {
            $.messager.progress();
            //if ($("#radioRole1").attr("checked") == "checked" && $("#MerID").val() == "0") {
            //    alert('商户管理员必须选择所管理商户！');
            //    $.messager.progress('close');
            //    return false;
            //}


            $("#fm").form('submit', {
                url: url,
                onSubmit: function () {
                    if (!$(this).form('validate')) {
                        $.messager.progress('close');
                        return false;
                    }
                },
                success: function (result) {
                    $.messager.progress('close');
                    if (result > 0) {
                        $("#dlg").dialog('close');
                        $.messager.alert('提示', '信息保存成功');
                        $("#Managerlist").datagrid('reload');
                    } else if (result == -5) {
                        $.messager.show({
                            title: 'Error',
                            msg: "该用户名已经存在"
                        }
                        );
                    }
                    else {
                        $.messager.show({
                            title: 'Error',
                            msg: "保存信息失败"
                        }
                        );
                    }
                }
            });
        }
        //编辑
        function editManager() {
            var row = $("#Managerlist").datagrid('getSelected');
            if (row) {
                $("#dlg").dialog('open').dialog('setTitle', '编辑管理员信息');
                $("#fm").form('load', row);
                url = "/Manager/UpdateManager";

                if (row.LoginDisabled) //禁用
                {
                    $("#radio1").removeAttr("checked");
                    $("#radio2").attr("checked", "checked");
                }
                else {
                    $("#radio2").removeAttr("checked");
                    $("#radio1").attr("checked", "checked");
                }
                if (row.RoleID == 0) {
                    $("#MerID").attr("disabled", "disabled");
                }

                $('#AdminPwd').validatebox({ required: false });
                $("#DivUserName").hide();
                $("#AdminPwd").val("");
                $("#pwdtip").html("不修改请留空！");

                $("#AdminID").val(row.AdminID);
            } else {

                $.messager.alert('提示', '请选择数据行进行编辑!');
            }
        }
        function delManager() {
            var row = $("#Managerlist").datagrid('getSelected');
            if (row) {
                $.messager.confirm('Confirm', '您确定要删除该数据吗?', function (r) {
                    if (r) {
                        $.post('/Manager/DelManager', { id: row.AdminID }, function (result) {
                            if (result == "ok") {
                                $("#Managerlist").datagrid('reload');
                            }
                            else {
                                $.messager.alert('提示', result);
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', '请选择数据行进行操作!');
            }
        }
    </script>
}
<div id="divsearch">
    用户名：<input type="text" name="UserName" id="UserName" style="width: 80px" />
    <a href="#" class="easyui-linkbutton" onclick="doSearch();" iconcls="icon-search">搜索</a>
</div>
<table id="Managerlist" toolbar="#divsearch">
</table>
<div id="dlg-buttons">
    <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="ManagerSave()">保存</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">
        取消</a>
</div>
<div id="dlg" class="easyui-dialog" style="width: 800px; padding: 10px 10px" resizable="true"
    closed="true" modal="true" buttons="#dlg-buttons">
    <form id="fm" method="post" enctype="multipart/form-data">
        <table style="width:100%">
            <tr>
                <td>角 色:</td>
                <td>
                    <input id="radioRole0" name="RoleID" type="radio" value="0" />系统管理员
                    <input id="radioRole1" name="RoleID" type="radio" value="1" /> 项目管理员
                    <input id="radioRole2" name="RoleID" type="radio" value="2" /> 批量充值管理员
                </td>
            </tr>
            <tr id="DivUserName">
                <td>用户名:</td>
                <td>@Html.TextBoxFor(m => m.AdminUserName, new { @class = "easyui-validatebox", @style = "width:200px;", @required = "true", @name = "AdminUserName" })</td>
            </tr>
            <tr>
                <td>密 码:</td>
                <td>@Html.TextBoxFor(m => m.AdminPwd, new { @class = "easyui-validatebox", @style = "width:200px;", @required = "true", @id = "AdminPwd", @name = "AdminPwd" })   <span id="pwdtip"></span></td>
            </tr>
            <tr>
                <td>姓 名:</td>
                <td>@Html.TextBoxFor(m => m.TrueName, new { @class = "easyui-validatebox", @style = "width:200px;" }) *管理员为项目管理员时请填写</td>
            </tr>
            <tr>
                <td>手机号:</td>
                <td>@Html.TextBoxFor(m => m.Mobile, new { @class = "easyui-validatebox", @maxlength = "11", @style = "width:200px;" })  *管理员为项目管理员时请填写</td>
            </tr>
           
            <tr>
                <td>账户状态:</td>
                <td>@Html.RadioButtonFor(m => m.LoginDisabled, false, new { @id = "radio1", @name = "LoginDisabled" })启用
                @Html.RadioButtonFor(m => m.LoginDisabled, true, new { @id = "radio2", @name = "LoginDisabled" })禁用
                <br /><span style="color:red;">注意：账户禁用时，只限制管理员登录管理平台，不限制相关项目的充值。</span>
                <input type="hidden" name="AdminID" id="AdminID" /></td>
            </tr>
        </table>  
    </form>
</div>
